package top.zxybird.common.constants;

import java.util.regex.Pattern;

public enum DeviceOsType {

    ios((byte) 1), android((byte) 2), wp((byte) 3), win((byte) 4), pc((byte) 5)/*pc浏览器，不区分系统类型。mobile中不判断。供web使用的接口中指定。*/;

    public static String IOS_CLIENT_PATTERN_STRING = "(.)*(Darwin|CFNetwork)(.)*";//

    public static Pattern IOS_CLIENT_PATTERN = Pattern.compile(IOS_CLIENT_PATTERN_STRING, Pattern.CASE_INSENSITIVE);

    public static String ANDROID_CLIENT_PATTERN_STRING = "^ting(.)*Android(\\d)*(.*)";//

    public static Pattern ANDROID_CLIENT_PATTERN = Pattern.compile(ANDROID_CLIENT_PATTERN_STRING,
            Pattern.CASE_INSENSITIVE);

    public static String WP_CLIENT_PATTERN_STRING = "^ting(.)*WP(.)*";

    public static Pattern WP_CLIENT_PATTERN = Pattern.compile(WP_CLIENT_PATTERN_STRING, Pattern.CASE_INSENSITIVE);

    private byte val;

    DeviceOsType(byte val) {
        this.setVal(val);
    }

    public static DeviceOsType parseDeviceTypeByUserAgent(String userAgent) {
        if (null == userAgent || userAgent.trim().equals("")) {
            return null;
        }
        if (IOS_CLIENT_PATTERN.matcher(userAgent).matches()) {
            return ios;
        } else if (ANDROID_CLIENT_PATTERN.matcher(userAgent).matches()) {
            return android;
        } else if (WP_CLIENT_PATTERN.matcher(userAgent).matches()) {
            return wp;
        } else {
            return null;
        }
    }

    public static DeviceOsType getDeviceOsTypeByVal(Byte val) {
        if (null != val) {
            for (DeviceOsType type : DeviceOsType.values()) {
                if (type.getVal() == val) {
                    return type;
                }
            }
        }
        throw new IllegalArgumentException("unknown device os type");
    }

    public byte getVal() {
        return val;
    }

    public void setVal(byte val) {
        this.val = val;
    }
}
